package ssm.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ssm.dao.CameraInfo;
import ssm.dao.OpenedState;
import ssm.dao.TreeJson;
import ssm.dao.Regioninfo;
import ssm.model.CameraInfoMapper;
import ssm.model.RegioninfoMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RegioninfoServiceImpl implements RegioninfoService {

    @Autowired
    private RegioninfoMapper regioninfoMapper;

    @Autowired
    private CameraInfoMapper cameraInfoMapper;

    public List<TreeJson> getTree() {

        List<TreeJson> list = new ArrayList<TreeJson>();

        List<Regioninfo> regioninfos = regioninfoMapper.get();

        TreeJson index = new TreeJson();
        index.setId("ajson0");
        index.setParent("#");
        index.setText("警察学院");
        index.setState(new OpenedState(true));
        index.setType("star");
        list.add(index);

        OpenedState state1 = new OpenedState(false);
        for (Regioninfo reg : regioninfos) {
            TreeJson treeJson = new TreeJson();
            treeJson.setId("ajson" + reg.getRegionid() + "");
            treeJson.setParent("ajson" + reg.getParentregionid() + "");
            treeJson.setText(reg.getName());
            treeJson.setState(state1);
            treeJson.setType("star");
            list.add(treeJson);
        }
/*
        List<Integer> regions = regioninfoMapper.getUpIndex();*/
        int count = 0;
        System.out.println(count);
        /*for(Integer reg:regions){
            List<CameraInfo> cameraInfoList = cameraInfoMapper.getByRegionId(reg);*/

        List<CameraInfo> cameraInfos = cameraInfoMapper.get();
        for (CameraInfo camer : cameraInfos) {
            TreeJson treeJson = new TreeJson();
            treeJson.setId(camer.getCameraid() + "");
            treeJson.setParent("ajson" + camer.getRegionid() + "");
            treeJson.setText(camer.getName());
            treeJson.setType("cloud");
            treeJson.setIndexCode(camer.getIndexcode());
            treeJson.setState(state1);
            treeJson.setCamerJd(camer.getCamerjd());
            treeJson.setCamerWd(camer.getCamerwd());
            treeJson.setSfsn(camer.getSfsn());
            treeJson.setPlayBackXml(camer.getPlaybackxml());
            treeJson.setPlayViewXml(camer.getPreviewxml());
            treeJson.setBuildName(camer.getBuildName());
            list.add(treeJson);
            count++;
        }
        /*}*/
        System.out.println(count);

        return list;
    }

    public Map<String, Object> getRegionAdnCarmer() {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Regioninfo> regs = regioninfoMapper.get();
        map.put("parent", regs);
        List<CameraInfo> camers = cameraInfoMapper.get();
        map.put("children", camers);
        return map;
    }

}
